import { Controller } from 'egg';

class AccusationController extends Controller {

    /**
     * 根据条件查询举报列表
     */
    public async searchAccusationAll() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                page: 'string',
                size: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const userList = await this.service.accusationService.searchAccusationAll(request.body);
        ctx.body = { code: 1, msg: '查询成功', data: userList };
    }

    /**
     * 获取系统默认禁言时间
     */
    public async getSystemExcuseTime() {
        const { ctx } = this;
        const result = await this.service.accusationService.getSystemExcuseTime();
        ctx.body = { code: 1, msg: '查询成功', data: result };
    }

    /**
     * 操作举报状态
     */
    public async editAccusationStatus() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                report_user_id: 'string',
                accusation_id: 'string',
                type: 'accusationType',
                accusation_status: 'string',
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0]['message']);
            return;
        }
        let type = Number(request.body.type);
        if (type === 3) {
            let anexcuse_time = Number(request.body.anexcuse_time);
            if (!anexcuse_time) {
                throw new Error(JSON.stringify({ code: 4, msg: '禁言时长必须为正整数' }));
            }
        }
        await this.service.accusationService.editAccusationStatus(request.body);
        ctx.body = { code: 1, msg: '操作成功' };
    }
}

export default AccusationController;
